package com.dayouzi.esaydata.controller;


import com.dayouzi.esaydata.base.exception.Assert;
import com.dayouzi.esaydata.base.result.R;
import com.dayouzi.esaydata.base.result.ResponseEnum;
import com.dayouzi.esaydata.pojo.vo.LoginDTO;
import com.dayouzi.esaydata.pojo.vo.LoginVO;
import com.dayouzi.esaydata.service.LoginService;
import com.dayouzi.esaydata.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(description = "登录管理")
@RestController
@RequestMapping("/flac/user")
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录
     *
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    public R login(
            @ApiParam(value = "登录信息", required = true) @Valid @RequestBody LoginDTO loginDTO
    ) {
        LoginVO loginVO = loginService.login(loginDTO);
        System.out.println(loginVO.getToken());
        return R.ok().data("result", loginVO);
    }

    /**
     * 信息
     *
     * @return
     */
    @GetMapping("/info")
    public R info(
            @ApiParam(value = "token", required = true) @RequestParam("token") String token
    ) {
        System.out.println(token);
        //校验token
        Assert.isTrue(JwtUtils.checkToken(token), ResponseEnum.TOKEN_ERROR);

        //获取用户角色
        String userName = JwtUtils.getUserName(token);
        String role[] = loginService.getRole(userName);
        return R.ok().data("roles", role)
                .data("name", userName)
                .data("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }

    /**
     * 注销
     *
     * @return
     */
    @PostMapping("/logout")
    public R logout() {
        log.info("注销成功");
        return R.ok();
    }


}
